package cn.com.dhcc.turbo.dao.registry;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import cn.com.dhcc.app.core.CM;
import cn.com.dhcc.app.core.dao.BaseDao;
import cn.com.dhcc.app.core.exception.DaoException;
import cn.com.dhcc.app.core.handler.modulehandler.WhereCondition;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.core.service.Log.LOGTYPE;
import cn.com.dhcc.turbo.entity.monitor.MonitorShowVO;
import cn.com.dhcc.turbo.entity.monitor.wapper.MonitorShowRowWapper;
import cn.com.dhcc.turbo.entity.registry.MonitorQuota;
import cn.com.dhcc.turbo.entity.registry.MonitorQuotaConf;
import cn.com.dhcc.turbo.entity.registry.wapper.MonitorQuotaRowWapper;

/**
 * 监控指标dao
 * 
 * @日期：2014-6-12 下午3:14:36
 * @作者：张洪涛
 * @版权所有：东华软件
 * @版本：1.0
 */
@Repository
public class MonitorQuotaDao extends BaseDao {
	@SuppressWarnings("deprecation")
	private static Logger logger = Log.getExistLogger(LOGTYPE.TURBO);

	/**
	 * 获取监控指标信息列表
	 * 
	 * @param pager
	 * @param condition
	 * @return
	 * @throws DaoException
	 */
	public List<MonitorQuota> list() throws DaoException {
		String msg = "";
		String sql = " select t1.id, t1.name, t1.value_sug1, t1.value_sug2, t2.rule_name,t1.rule_sug,t1.status"
					+ "     from  " + SI_TABLE("turbo_monitor_quota") + "  t1"
					+ "     left join  " + SI_TABLE("turbo_monitor_rule") + "  t2 on t2.sn = t1.rule_sug"
					+ "   	where 	t1.id not in (select t6.quota_id from  " + SI_TABLE("turbo_monitor_quota_conf") + "  t6)"
					+ "   		and t1.modifiable = '1'"
					+ "   union " 
					+ "   	select t4.id, t4.name,t3.value1    value_sug1, t3.value2    value_sug2, t5.rule_name, t3.rule_sn,t4.status"
					+ "    	from  " + SI_TABLE("turbo_monitor_quota_conf") + "  t3"
					+ "   	left join  " + SI_TABLE("turbo_monitor_quota") + "  t4" + "   	 on t3.quota_id = t4.id" 
					+ "   	left join  " + SI_TABLE("turbo_monitor_rule") + "  t5 on t3.rule_sn = t5.sn";
		try {
			return CM.getDao().list("获取监控指标信息列表", sql,
					new MonitorQuotaRowWapper(), new Object[] {});
		} catch (DaoException e) {
			msg = "获取监控指标信息列表失败";
			logger.error(msg, e);
			throw new DaoException(msg, e);
		}
	}

	/**
	 * 获取监控指标信息
	 * 
	 * @param id
	 * @return
	 * @throws DaoException
	 */
	public MonitorQuota get(String id) throws DaoException {
		String msg = "";
		String sql = "select t1.id,  t1.name, " + "   case "
				+ "   when t2.id is null then" + "        t1.rule_sug "
				+ "  	else " + "          t2.rule_sn "
				+ "        end rule_sug,t1.alter_template_sug,"
				+ "  t1.value_sug1," + "  t1.value_sug2," + "  t2.value1,"
				+ "  t2.value2," + "  t2.id confid," + "   case "
				+ "   when t2.id is null then" + "        t1.alter_pipeline "
				+ "    else " + "          t2.alter_pipeline " + "    end "
				+ " 			alter_pipeline ," + "   case "
				+ "   when t2.id is null then" + "        t1.remark "
				+ "         else " + "          t2.remark "
				+ "        end remark" + " from "
				+ SI_TABLE("turbo_monitor_quota") + " t1" + "  left join "
				+ SI_TABLE("turbo_monitor_quota_conf") + " t2"
				+ "    on t2.quota_id = t1.id" + "    where t1.id=?";
		try {
			return CM.getDao().get("获取监控指标信息", sql,
					new MonitorQuotaRowWapper(), new Object[] { id });
		} catch (DaoException e) {
			msg = "获取监控指标信息失败";
			logger.error(msg, e);
			throw new DaoException(msg, e);
		}
	}

	/**
	 * 自定義指標
	 * 
	 * @param vo
	 * @return
	 * @throws DaoException
	 */
	public int insertMonitorQuota(MonitorQuotaConf vo) throws DaoException {
		String msg = "";
		String sql = "insert into "
				+ SI_TABLE("turbo_monitor_quota_conf")
				+ " (id,quota_id,rule_sn,value1,value2,alter_template,mtime,remark,alter_pipeline) values(?,?,?,?,?,?,?,?,?)";
		try {
			return CM.getDao().insert(
					"修改指标",
					sql,
					new Object[] { vo.getId(), vo.getQuotaId(), vo.getRuleSn(),
							vo.getValue1(), vo.getValue2(),
							vo.getAlterTemplate(), vo.getMtime(),
							vo.getRemark(), vo.getAlterPipeline() });
		} catch (DaoException e) {
			msg = "修改指标失败";
			logger.error(msg, e);
			throw new DaoException(msg, e);
		}
	}

	/**
	 * 修改指標
	 * 
	 * @param vo
	 * @return
	 * @throws DaoException
	 */
	public int updateMonitorQuota(MonitorQuotaConf vo) throws DaoException {
		String msg = "";
		String sql = "update "
				+ SI_TABLE("turbo_monitor_quota_conf")
				+ " set rule_sn=?,value1=?,value2=?,alter_template=?,mtime=?,remark=?,alter_pipeline=? where id=? and quota_id=?";
		try {
			return CM.getDao()
					.update("修改指标",
							sql,
							new Object[] { vo.getRuleSn(), vo.getValue1(),
									vo.getValue2(), vo.getAlterTemplate(),
									vo.getMtime(), vo.getRemark(),
									vo.getAlterPipeline(), vo.getId(),
									vo.getQuotaId() });
		} catch (DaoException e) {
			msg = "修改指标失败";
			logger.error(msg, e);
			throw new DaoException(msg, e);
		}
	}

	/**
	 * 修改指标状态
	 * 
	 * @param id
	 * @param status
	 * @return
	 * @throws DaoException
	 */
	public int updateStatus(String id, String status) throws DaoException {
		String msg = "";
		String sql = "update " + SI_TABLE("turbo_monitor_quota")
				+ " set status=? where id=?";
		try {
			return CM.getDao().update("修改指标状态", sql,
					new Object[] { status, id });
		} catch (DaoException e) {
			msg = "修改指标状态";
			logger.error(msg, e);
			throw new DaoException(msg, e);
		}
	}

	/**
	 * 通过指标编码获取指标
	 * 
	 * @param code
	 * @return
	 * @throws DaoException
	 */

	public List<MonitorShowVO> getMonitorShow(String[] codes,String ownerId) throws DaoException {
		WhereCondition condition = new WhereCondition();
		if(codes!=null&&codes.length!=0){
			condition.and().in("t1.quota_code", codes);
		}
		if (!"".equals(ownerId) && ownerId != null) {
			condition.and().eq("t1.owner_id", ownerId);
		}
		try {
			String sql = " select t1.quota_code code, t1.current_value1 value, t1.needalert alert,t1.mtime"
								+ " from  " + SI_TABLE("turbo_monitor_ontime")+" t1 where t1.mtime=(select max(mtime)  from turbo_monitor_ontime  where owner_id = t1.owner_id and quota_code = t1.quota_code)";
			return CM.getDao().list("获取监控指标信息", sql,
					new MonitorShowRowWapper(), condition, new Object[] {});
		} catch (DaoException e) {
			throw new DaoException("获取监控指标信息失败", e);
		}
	}
}
